#!/usr/bin/env python
# -*- coding: utf-8 -*-
import platform
import time
import distro
from s2c.rules.conf import MIGRATE_MAPPING, SYS_CURRENT


# SystemInfo 系统信息采集
class SystemInfo:

    def __init__(self):
        uname = platform.uname()
        self.name = distro.id()
        self.ver = distro.version()
        self.arch = uname.machine
        self.sdk = uname.version
        self.version = 'V1.0.0'  # 暂时使用默认版本
        time_now = int(time.time())  # 获取当前时间的时间戳
        ltime = time.localtime(time_now)
        self.create_time = time.strftime("%Y-%m-%d %H:%M:%S", ltime)

    def obj(self):
        return {
            'name': self.name,
            'arch': self.arch,
            'ver': self.ver,
            'sdk': self.sdk,
            # 'version': self.version,
            'create_time': self.create_time,
        }

    def dump(self):
        obj = self.obj()
        obj['file_name'] = self.file_name()
        obj['process_name'] = self.process_name()
        obj['target_system'] = self.current_system()
        print("系统信息：")
        for key, val in obj.items():
            print(key + ": " + val)

    def prefix(self, is_target=False):
        key = self.current_system()
        if is_target:
            val = self.target_system()
            if key != val:
                key = val

        return '_'.join([key, self.arch])  # + '_' + self.version

    # 拼接文件名称
    def file_name(self, is_target=False, dict_type=SYS_CURRENT):
        return self.prefix(is_target) + f'_file.{dict_type}.json'

    # 返回系统正在运行的进程
    def process_name(self, is_target=False, dict_type=SYS_CURRENT):
        return self.prefix(is_target) + f'_process.{dict_type}.json'

    # 当前系统 name + ver
    def current_system(self):
        return self.name + '_' + self.ver

    # 目标系统 name + ver
    def target_system(self):
        key = self.current_system()
        if key in MIGRATE_MAPPING.keys():
            targets = MIGRATE_MAPPING[key]
            if isinstance(targets, list) and len(targets) > 0:
                return targets[0]  # 优先选择第一个
        return key

    def need_target(self):
        return self.current_system() != self.target_system()


if __name__ == "__main__":
    SystemInfo().dump()
